/*
 * Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3
 */

#include "Chat.h"
#include "ChatCommand.h"
#include "CommandScript.h"
#include "Common.h"
#include "Config.h"
#include "Player.h"
#include "PlayerLevelUpGiftScript.h"
#include "PlayerGift.h"
#include <vector>
#include <cstdlib>
#include "World.h"
#include "WorldSession.h"
#include "WorldSessionMgr.h"

using namespace Acore::ChatCommands;

extern PlayerLevelUpGiftScript *playerLevelUpGiftScript;
// Add player scripts
class PlayerGiftCommandScript : public CommandScript
{
public:
    PlayerGiftCommandScript() : CommandScript("PlayerGiftCommandScript") {}

    ChatCommandTable GetCommands() const override
    {
        static ChatCommandTable KkkGiftCommandTable =
        {
            {"reloadgift",  HandleKKKGiftReloadCommand, SEC_GAMEMASTER, Console::Yes},
            {"rename",      HandleKKKGiftRenameCommand, SEC_PLAYER,     Console::No},
            {"xprate",      HandleKKKXPRateCommand,     SEC_GAMEMASTER, Console::No},  // .kkk xprate {rate value /* not null */} {days /* nullable */}
            {"info",      HandleKKKGiftInfoCommand, SEC_PLAYER,     Console::No},
            {"hardOff",   HandleKKKGiftHardOffCommand, SEC_GAMEMASTER,     Console::No} ,//Disable hard mode
            {"hard",      HandleKKKGiftHardCommand, SEC_PLAYER,     Console::No} //Enable hard mode
        };

        static ChatCommandTable commandTable =
        {
            {"kkk", KkkGiftCommandTable}
        };

        return commandTable;
    }
    static bool HandleKKKXPRateCommand(ChatHandler *handler, const char*args)
    {
        if( args == nullptr){
            handler->PSendSysMessage("{}","usage : .kkk xprate {rate value /* not null ,Percentage value!*/} {days /* nullable */}");
            return true;
         }

        Player *player = handler->GetPlayer();
        ObjectGuid targetGuid = player->GetTarget();
        Player * targetPlayer = player->GetSelectedPlayer();
        if( targetGuid.IsEmpty()){
            handler->PSendSysMessage("{}","Target player is empty!");
            return true;
        }

        if( !targetGuid.IsPlayer()){
            handler->PSendSysMessage("{}","Target must be player!");
            return true;
        }
        
        char* rateRateValue  = strtok((char*)args, " ");

        if(rateRateValue == nullptr ){
            handler->PSendSysMessage("{}","rateRateValue is empty!");
            return true;
        }
        int xpRateIntValue = atoi(rateRateValue);
        char* days           = strtok(NULL, " ");
        
        playerLevelUpGiftScript->setXpRate(player->GetGUID().GetRawValue(),
                                            targetGuid.GetRawValue(),
                                            xpRateIntValue,
                                            days == nullptr ? 0 :atoi(days),true);
        handler->PSendSysMessage("将 >>{}<< 的经验倍率调整为：{} %!",targetPlayer->GetName(),xpRateIntValue);
        ChatHandler(targetPlayer->GetSession()).PSendSysMessage("经验倍率被 >>{}<< 调整为：{} %",  player->GetName() ,xpRateIntValue);
        return true;
    }


    static bool HandleKKKGiftInfoCommand(ChatHandler *handler)
    {
        vector<LevelUpGift *>  gifts = playerLevelUpGiftScript->getGifts();
        int i = 0;
        handler->PSendSysMessage("{}","--------------------------- Gift Info ---------------------------");
        
        for(i = 0 ; i < gifts.size() ; i++){
            LevelUpGift * gift = gifts[i];
            if(gift == nullptr) continue;
            handler->PSendSysMessage("Level:{}      Type > {} <      Subject:{}",gift->getTargetLevel(),"Levelup Gift",gift->getSubject());
        }
        handler->PSendSysMessage("{}","------------------------------ END -----------------------------");

        gifts = playerLevelUpGiftScript->getAlleEchantingGift();
        i = 0;
        handler->PSendSysMessage("{}","--------------------- Enchating Gift Info ----------------------");
        
        for(i = 0 ; i < gifts.size() ; i++){
            LevelUpGift * gift = gifts[i];
            if(gift == nullptr) continue;
            handler->PSendSysMessage("Level:{}      Type > {} <      Subject:{}",gift->getTargetLevel(),"Enchanting Gift",gift->getSubject());
        }
        handler->PSendSysMessage("{}","------------------------------ END -----------------------------");
        return true;
    }

    static bool HandleKKKGiftReloadCommand(ChatHandler *handler)
    {
        handler->PSendSysMessage("{}","KKK GIFT Reload start!");
        playerLevelUpGiftScript->init();
        handler->PSendSysMessage("{}","KKK GIFT Reload end!");
        return true;
    }

    static bool HandleKKKGiftRenameCommand(ChatHandler *handler)
    {
        handler->PSendSysMessage("{}","KKK GIFT: >>>> 使用改名卡!");
        // check if the player can do  rename action
       
        PlayerSetting ps = handler->GetPlayer()->GetPlayerSetting(KKK_GIFT_SOURCE, KKK_GIFT_RENAME_INX);
        bool canDoRename =  ps.IsEnabled();

        if( canDoRename ){
            handler->GetPlayer()->SetAtLoginFlag(AT_LOGIN_RENAME);
            handler->PSendSysMessage("{}","KKK GIFT: >>>> 改名卡使用成功，下次登录时可以修改名字!");
            handler->GetPlayer()->UpdatePlayerSetting(KKK_GIFT_SOURCE,KKK_GIFT_RENAME_INX,0);
        }else{
            handler->PSendSysMessage("{}","KKK GIFT: >>>> 改名卡使用失败!(你还没有获得改名卡)");
        }
        return true;
    }

    static bool HandleKKKGiftHardCommand(ChatHandler *handler)
    {
        PlayerSetting ps = handler->GetPlayer()->GetPlayerSetting(KKK_GIFT_SOURCE, KKK_GIFT_HARD_MODE_INX);
        if(ps.HasFlag(KKK_GIFT_HARD_MODE_ENABLE)){
            handler->PSendSysMessage("{}","KKK GIFT: >>>> 你已经处于一命模式中，无须再次开启!");
            return true;
        }
        handler->PSendSysMessage("{}","KKK GIFT: >>>> 开启一命模式!");
        uint32 value = ps.value | KKK_GIFT_HARD_MODE_ENABLE;
        ps.AddFlag(KKK_GIFT_HARD_MODE_ENABLE);
        handler->GetPlayer()->UpdatePlayerSetting(KKK_GIFT_SOURCE,KKK_GIFT_HARD_MODE_INX,value);
        handler->PSendSysMessage("{}","KKK GIFT: >>>>开启一命模式成功!");
        sWorldSessionMgr->SendServerMessage(SERVER_MSG_STRING,"江湖中又出现了一位硬核勇士 >>>>" +  handler->GetPlayer()->GetName() + "<<<< " );
        return true;
    }
    static bool HandleKKKGiftHardOffCommand(ChatHandler *handler, const char*args)
    {
       
        Player *player = handler->GetPlayer();
        ObjectGuid targetGuid = player->GetTarget();
        Player * targetPlayer = player->GetSelectedPlayer();
        if( targetGuid.IsEmpty()){
            handler->PSendSysMessage("{}","Target player is empty!");
            return true;
        }

        if( !targetGuid.IsPlayer()){
            handler->PSendSysMessage("{}","Target must be player!");
            return true;
        }
        
        handler->PSendSysMessage("{}",">>>> 关闭一命模式!");

        targetPlayer->UpdatePlayerSetting(KKK_GIFT_SOURCE,KKK_GIFT_HARD_MODE_INX,0);

        handler->PSendSysMessage("{}",">>>>关闭一命模式成功!");
      
                                         
        handler->PSendSysMessage("将 >>{}<< 的一命模式关闭!",targetPlayer->GetName());
        ChatHandler(targetPlayer->GetSession()).PSendSysMessage("一命模式被 >>{}<< 关闭",  player->GetName());
        sWorldSessionMgr->SendServerMessage(SERVER_MSG_STRING,"江湖中失去了一位硬核勇士 >>>>" +  handler->GetPlayer()->GetName() + "<<<< " );
        return true;
    }
    
};

// Add all scripts in one
void kkkgiftCommandScripts()
{
    new PlayerGiftCommandScript();
}
